from flask import render_template,redirect,request,url_for,flash
from flask_login import login_user,logout_user,login_required
from . import auth
from ..models import Administrator
from .. import db
from .forms import AdminLoginForm

@auth.route('/login',methods=['GET','POST'])
def login():
    form = AdminLoginForm()
    if form.validate_on_submit():
        # admin = Administrator(username=form.username.data,password=form.password.data)
        # db.session.add(admin)
        # db.session.commit()

        admin = Administrator.query.filter_by(username=form.username.data).first()
        if admin is not None and admin.verify_password(form.password.data):# 验证成功
            login_user(admin,form.remember_me.data)
            return redirect(url_for('main.manage_news'))
    return render_template('auth/login.html',form=form)

@auth.route('/logout',methods=['GET','POST'])
@login_required
def logout():
    logout_user()
    return redirect(url_for('auth.login'))
